package com.emm.yixun.website.controller.repairData;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.emm.yixun.common.model.LevelStaticSalesData;
import com.emm.yixun.common.model.LevelStaticSalesDataChild;
import com.emm.yixun.common.model.PmoLog;
import com.emm.yixun.common.model.ProjectInfo;
import com.emm.yixun.common.model.ProjectPmo;
import com.emm.yixun.common.model.ProjectPremises;
import com.emm.yixun.common.model.ProjectPremisesInfo;
import com.emm.yixun.common.model.ProjectPremisesPmo;
import com.emm.yixun.common.model.StaticSalesData;
import com.emm.yixun.common.model.dto.ProjectDto;
import com.emm.yixun.common.model.dto.ProjectInfoDto;
import com.emm.yixun.common.model.dto.ProjectPmoDto;
import com.emm.yixun.common.model.dto.ProjectPremisesDto;
import com.emm.yixun.common.model.dto.ProjectPremisesInfoDto;
import com.emm.yixun.common.model.dto.ProjectPremisesPmoDto;
import com.emm.yixun.common.model.pmo.TargetAveragePrice;
import com.emm.yixun.common.model.pmo.TargetPriceOfParkinSpace;
import com.emm.yixun.common.model.pmo.TargetValue;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.common.utils.DateStyle;
import com.emm.yixun.common.utils.DateUtils;
import com.emm.yixun.estate.service.ProjectInfoServiceFacade;
import com.emm.yixun.estate.service.ProjectPmoServiceFacade;
import com.emm.yixun.estate.service.ProjectPremisesInfoServiceFacade;
import com.emm.yixun.estate.service.ProjectPremisesPmoServiceFacade;
import com.emm.yixun.estate.service.ProjectPremisesServiceFacade;
import com.emm.yixun.estate.service.ProjectServiceFacade;
import com.emm.yixun.system.service.PmoLogServiceFacade;

@Controller
@RequestMapping("/repair/")
public class RepairStaticDataController {
	@Autowired
	private PmoLogServiceFacade pmoLogServiceFacade;
	@Autowired
	private ProjectPmoServiceFacade projectPmoServiceFacade;
	@Autowired
	private ProjectServiceFacade projectServiceFacade;
	@Autowired
	private ProjectInfoServiceFacade projectInfoServiceFacade;
	@Autowired
	private ProjectPremisesServiceFacade premisesServiceFacade;
	@Autowired
	private ProjectPremisesInfoServiceFacade projectPremisesInfoServiceFacade;
	@Autowired
	private ProjectPremisesPmoServiceFacade projectPremisesPmoServiceFacade;
	
	@RequestMapping("staticData")
	@ResponseBody
	public String staticData(Long id){
		//查询销售主数据日志
		//修改项目、分期级别静态信息
		Response<PmoLog> pmoLogResponse=pmoLogServiceFacade.findById(id);
		if(!pmoLogResponse.isSuccess()){
			return "日志("+id+")不存在";
		}
		PmoLog pmoLog=pmoLogResponse.getEntity();
		if(null==pmoLog){
			return "日志("+id+")不存在";
		}
		String requestParams=pmoLog.getRequestParams();
		if(StringUtils.isBlank(requestParams)){
			return "日志对象请求参数属性为空";
		}
		try {
			LevelStaticSalesData levelStaticSalesData=JSONObject.parseObject(requestParams, LevelStaticSalesData.class);
			repairProjectStaticData(levelStaticSalesData);
			repairPremisesStaticData(levelStaticSalesData);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return e.getMessage();
		}
		return "success";
	}
	
	//修复项目静态数据
	private void repairProjectStaticData(LevelStaticSalesData levelStaticSalesData){
		if(null==levelStaticSalesData)return;
		String pmoProjectCode=levelStaticSalesData.getProjectCode();
		if(StringUtils.isBlank(pmoProjectCode))return;
		StaticSalesData staticSalesData=levelStaticSalesData.getData();
		if(null==staticSalesData)return;
		//查询项目
		ProjectPmoDto queryPmo=new ProjectPmoDto();
		queryPmo.setPmoProjectCode(pmoProjectCode);
		ResponseList<ProjectPmo> ppList=projectPmoServiceFacade.findByDto(queryPmo);
		if(!ppList.isSuccess() || null==ppList.getEntity() || ppList.getEntity().size()==0)return;
		ProjectPmo projectPmo=ppList.getEntity().get(0);
		if(null==projectPmo)return;
		ProjectDto projectUpdate=new ProjectDto();
		projectUpdate.setId(projectPmo.getProjectId());
		//车位配比
		projectUpdate.setCarportMatching(staticSalesData.getParkinRatio());
		//车位数
		projectUpdate.setCarportTotal(staticSalesData.getNumberOfParkinSpace());
		projectServiceFacade.updateByPrimaryKey(projectUpdate, true);
		
		ProjectPmoDto projectPmoDtoUpdate=new ProjectPmoDto();
		projectPmoDtoUpdate.setId(projectPmo.getId());
		//自持面积
		if(null!=staticSalesData.getSelfSustaiAcreag())
			projectPmoDtoUpdate.setSelfSustaiAcreag(new BigDecimal(staticSalesData.getSelfSustaiAcreag()));
		//栋数
		projectPmoDtoUpdate.setNumberOfBuildi(staticSalesData.getNumberOfBuildi());
		//可售套数
		projectPmoDtoUpdate.setSalableNumber(staticSalesData.getSalableNumber());
		//地上可售面积
		if(null!=staticSalesData.getGroundSalesAcreag()){
			projectPmoDtoUpdate.setGroundSalesAcreag(new BigDecimal(staticSalesData.getGroundSalesAcreag()));
		}
		
		//地下可售面积
		if(null!=staticSalesData.getUndergSalesAcreag()){
			projectPmoDtoUpdate.setUndergSalesAcreag(new BigDecimal(staticSalesData.getUndergSalesAcreag()));
		}
		//目标销售均价
		List<TargetAveragePrice> targetAveragPrice=staticSalesData.getTargetAveragPrice();
		if(null!=targetAveragPrice && targetAveragPrice.size()>0){
			TargetAveragePrice first=targetAveragPrice.get(0);
			if(null!=first.getTargetAveragPrice())
			projectPmoDtoUpdate.setTargetAveragPrice(new BigDecimal(first.getTargetAveragPrice()));
			if(StringUtils.isNotBlank(first.getEffectStartTime()))
			projectPmoDtoUpdate.setTargetAveragPriceTime(DateUtils.StringToDate(first.getEffectStartTime(), DateStyle.YYYYMMDD));
			projectPmoDtoUpdate.setTargetAveragPriceVersion(first.getTargetAveragPriceVersion());
		}
		//车位目标销售均价
		List<TargetPriceOfParkinSpace> targetPriceOfParkinSpace=staticSalesData.getTargetPriceOfParkinSpace();
		if(null!=targetPriceOfParkinSpace && targetPriceOfParkinSpace.size()>0){
			TargetPriceOfParkinSpace first=targetPriceOfParkinSpace.get(0);
			if(null!=first.getTargetPriceOfParkinSpace()){
				projectPmoDtoUpdate.setTargetPriceOfParkinSpace(new BigDecimal(first.getTargetPriceOfParkinSpace()));
			}
			if(StringUtils.isNotBlank(first.getEffectStartTime())){
				projectPmoDtoUpdate.setTargetPriceOfParkinSpaceTime(DateUtils.StringToDate(first.getEffectStartTime(), DateStyle.YYYYMMDD));
			}
			projectPmoDtoUpdate.setTargetPriceOfParkinVersion(first.getTargetPriceOfParkinVersion());
		}
		//目标货值
		List<TargetValue> targetValue=staticSalesData.getTargetValue();
		if(null!=targetValue && targetValue.size()>0){
			TargetValue first=targetValue.get(0);
			if(null!=first.getTargetValue()){
				projectPmoDtoUpdate.setTargetValue(new BigDecimal(first.getTargetValue()));
			}
			if(StringUtils.isNotBlank(first.getEffectStartTime())){
				projectPmoDtoUpdate.setTargetValueTime(DateUtils.StringToDate(first.getEffectStartTime(), DateStyle.YYYYMMDD));
			}
			projectPmoDtoUpdate.setTargetValueVersion(first.getTargetValueVersion());
		}
		projectPmoServiceFacade.updateByPrimaryKey(projectPmoDtoUpdate, true);
		
		ProjectInfoDto projectInfoDto=new ProjectInfoDto();
		projectInfoDto.setProjectId(projectPmo.getProjectId());
		ResponseList<ProjectInfo> piList=projectInfoServiceFacade.findByDto(projectInfoDto);
		if(null==piList)throw new RuntimeException("ProjectInfo未找到");
		ProjectInfo projectInfo=piList.getEntity().get(0);
		
		ProjectInfoDto projectInfoUpdate=new ProjectInfoDto();
		projectInfoUpdate.setId(projectInfo.getId());
		//地上机动车位
		projectInfoUpdate.setGroundMotor(staticSalesData.getGroundVechicParkinSpace());
		//地下机动车位
		projectInfoUpdate.setUndergroundMotor(staticSalesData.getUndergVechicParkinSpace());
		//地上非机动车位
		projectInfoUpdate.setGroundNonMotor(staticSalesData.getGroundNonVechicParkinSpace());
		//地下非机动车位
		projectInfoUpdate.setUndergroundNonMotor(staticSalesData.getUndergNonVechicParkinSpace());
		//可售面积
		if(null!=staticSalesData.getSalableAcreag())
			projectInfoUpdate.setContactArea(new BigDecimal(staticSalesData.getSalableAcreag()));
		//总户数
		projectInfoUpdate.setTotalHouse(staticSalesData.getTotalHouse());
		projectInfoServiceFacade.updateByPrimaryKey(projectInfoUpdate, true);	
	}
	//修复分期静态数据
	private void repairPremisesStaticData(LevelStaticSalesData levelStaticSalesData){
		if(null==levelStaticSalesData)return;
		List<LevelStaticSalesDataChild> staticSalesDatas=levelStaticSalesData.getStaticSalesDatas();
		if(null==staticSalesDatas)return;
		for(LevelStaticSalesDataChild child : staticSalesDatas){
			String pmoPremisesCode=child.getProjectCode();
			if(StringUtils.isBlank(pmoPremisesCode))continue;
			StaticSalesData staticSalesData=child.getData();
			if(null==staticSalesData)continue;
			ProjectPremisesPmoDto pppQuery=new ProjectPremisesPmoDto();
			pppQuery.setPmoPremisesCode(pmoPremisesCode);
			ResponseList<ProjectPremisesPmo> list=projectPremisesPmoServiceFacade.findByDto(pppQuery);
			if(!list.isSuccess() || null==list.getEntity() || list.getEntity().size()==0)continue;
			ProjectPremisesPmo projectPremisesPmo=list.getEntity().get(0);
			if(null==projectPremisesPmo)continue;
			Response<ProjectPremises> ppResponse=premisesServiceFacade.findById(projectPremisesPmo.getPremisesId());
			if(ppResponse.isSuccess() && null!=ppResponse.getEntity() ){
				ProjectPremises projectPremises=ppResponse.getEntity();
				
				ProjectPremisesDto projectPremisesDtoUpdate=new ProjectPremisesDto();
				projectPremisesDtoUpdate.setId(projectPremisesPmo.getPremisesId());
				//车位配比
				projectPremisesDtoUpdate.setCarportMatching(staticSalesData.getParkinRatio());
				//车位数
				projectPremisesDtoUpdate.setCarportTotal(staticSalesData.getNumberOfParkinSpace());
				premisesServiceFacade.updateByPrimaryKey(projectPremisesDtoUpdate, true);
				
				ProjectPremisesInfoDto ppiQuery=new ProjectPremisesInfoDto();
				ppiQuery.setPremisesId(projectPremises.getId());
				ResponseList<ProjectPremisesInfo> ppiList=projectPremisesInfoServiceFacade.findByDto(ppiQuery);
				if(ppiList.isSuccess() && null!=ppiList.getEntity() && ppiList.getEntity().size()>0){
					
					ProjectPremisesInfoDto infoDto=new ProjectPremisesInfoDto(); 
					infoDto.setId(ppiList.getEntity().get(0).getId());
					
					//总户数
					infoDto.setTotalHouse(staticSalesData.getTotalHouse());
					//自持面积
					if(null!=staticSalesData.getSelfSustaiAcreag())
						infoDto.setOwnArea(new BigDecimal(staticSalesData.getSelfSustaiAcreag()));

					//可售面积
					if(null!=staticSalesData.getSalableAcreag())
						infoDto.setHousingArea(new BigDecimal(staticSalesData.getSalableAcreag()));
					//地上可售面积
					if(null!=staticSalesData.getGroundSalesAcreag()){
						infoDto.setGroundArea(new BigDecimal(staticSalesData.getGroundSalesAcreag()));
					}
					
					//地下可售面积
					if(null!=staticSalesData.getUndergSalesAcreag()){
						infoDto.setUndergroundArea(new BigDecimal(staticSalesData.getUndergSalesAcreag()));
					}
					//地上机动车位
					infoDto.setGroundMotor(staticSalesData.getGroundVechicParkinSpace());
					//地下机动车位
					infoDto.setUndergroundMotor(staticSalesData.getUndergVechicParkinSpace());
					//地上非机动车位
					infoDto.setGroundNonMotor(staticSalesData.getGroundNonVechicParkinSpace());
					//地下非机动车位
					infoDto.setUndergroundNonMotor(staticSalesData.getUndergNonVechicParkinSpace());
					projectPremisesInfoServiceFacade.updateByPrimaryKey(infoDto, true);
				}else{
					throw new RuntimeException("ProjectPremisesInfo("+projectPremises.getId()+")未找到");
				}
				ProjectPremisesPmoDto pppUpdate=new ProjectPremisesPmoDto();
				pppUpdate.setId(projectPremisesPmo.getId());

				//栋数
				pppUpdate.setNumberOfBuildi(staticSalesData.getNumberOfBuildi());
				//可售套数
				pppUpdate.setSalableNumber(staticSalesData.getSalableNumber());
				//目标销售均价
				List<TargetAveragePrice> targetAveragPrice=staticSalesData.getTargetAveragPrice();
				if(null!=targetAveragPrice && targetAveragPrice.size()>0){
					TargetAveragePrice first=targetAveragPrice.get(0);
					if(null!=first.getTargetAveragPrice())
						pppUpdate.setTargetAveragPrice(new BigDecimal(first.getTargetAveragPrice()));
					if(StringUtils.isNotBlank(first.getEffectStartTime()))
						pppUpdate.setTargetAveragPriceTime(DateUtils.StringToDate(first.getEffectStartTime(), DateStyle.YYYYMMDD));
					pppUpdate.setTargetAveragPriceVersion(first.getTargetAveragPriceVersion());
				}
				//车位目标销售均价
				List<TargetPriceOfParkinSpace> targetPriceOfParkinSpace=staticSalesData.getTargetPriceOfParkinSpace();
				if(null!=targetPriceOfParkinSpace && targetPriceOfParkinSpace.size()>0){
					TargetPriceOfParkinSpace first=targetPriceOfParkinSpace.get(0);
					if(null!=first.getTargetPriceOfParkinSpace()){
						pppUpdate.setTargetPriceOfParkinSpace(new BigDecimal(first.getTargetPriceOfParkinSpace()));
					}
					if(StringUtils.isNotBlank(first.getEffectStartTime())){
						pppUpdate.setTargetPriceOfParkinSpaceTime(DateUtils.StringToDate(first.getEffectStartTime(), DateStyle.YYYYMMDD));
					}
					pppUpdate.setTargetPriceOfParkinVersion(first.getTargetPriceOfParkinVersion());
				}
				//目标货值
				List<TargetValue> targetValue=staticSalesData.getTargetValue();
				if(null!=targetValue && targetValue.size()>0){
					TargetValue first=targetValue.get(0);
					if(null!=first.getTargetValue()){
						pppUpdate.setTargetValue(new BigDecimal(first.getTargetValue()));
					}
					if(StringUtils.isNotBlank(first.getEffectStartTime())){
						pppUpdate.setTargetValueTime(DateUtils.StringToDate(first.getEffectStartTime(), DateStyle.YYYYMMDD));
					}
					pppUpdate.setTargetValueVersion(first.getTargetValueVersion());
				}
				projectPremisesPmoServiceFacade.updateByPrimaryKey(pppUpdate, true);
			}else{
				throw new RuntimeException("ProjectPremises("+projectPremisesPmo.getPremisesId()+")未找到");
			}
		}
		
	}
}
